# -*- coding: utf-8 -*-
# @Author   : niejiali
# @Time     : 2019/1/8 17:40
# @File     : email.py
# @Software : PyCharm
from threading import Thread

from flask import current_app, render_template

from app import mail

from flask_mail import Message


def send_async_email(app, msg):
    with app.app_context():
        try:
            mail.send(msg)
        except Exception as e:
            pass


def send_mail(to, subject, template, **kwargs):
    """

    :param to: 那个用户发送
    :param subject:标题
    :param template: 模板
    :param kwargs:temp一组参数
    :return:
    """
    # python email
    # msg = Message('测试邮件', sender='1073438012@qq.com', body='Test',
    #               recipients=['1073438012@qq.com'])
    msg = Message('[鱼书]' + ' ' + subject,
                  sender=current_app.config['MAIL_USERNAME'],
                  recipients=[to])
    msg.html = render_template(template, **kwargs)
    # app  = Flask()
    app = current_app._get_current_object()
    thr = Thread(target=send_async_email, args=[app, msg])
    thr.start()
    # mail.send(msg)

# send_mail()